using System;
using UnityEngine;

public class EnergyManager : BehaviourSingleton<EnergyManager>
{
	public float GetCurrentEnergyPercent()
	{
		return GetCurrentEnergyValue() / EnergyControlConfig.instance.maxEnergy;
	}

	private void RefreshEnergy()
	{
		if (!(GGPlayerSettings.instance.Model.energy >= EnergyControlConfig.instance.maxEnergy))
		{
			DateTime value = new DateTime(GGPlayerSettings.instance.Model.lastTimeTookEnergy);
			TimeSpan timeSpan = DateTime.Now.Subtract(value);
			if (!(timeSpan.TotalSeconds < (double)EnergyControlConfig.instance.secondsToRefreshPoint))
			{
				float energyForTimespan = EnergyControlConfig.instance.GetEnergyForTimespan(timeSpan);
				float num = Mathf.Min(energyForTimespan, EnergyControlConfig.instance.maxEnergy - GGPlayerSettings.instance.Model.energy);
				TimeSpan value2 = EnergyControlConfig.instance.TimeToGainEnergy(num);
				DateTime lastTimeTookEnergy = value.Add(value2);
				GGPlayerSettings.instance.UpdateEnergy(num, lastTimeTookEnergy);
			}
		}
	}

	public bool HasEnoughEnergy(float energyCost)
	{
		return GetCurrentEnergyValue() >= energyCost;
	}

	public float GetCurrentEnergyValue()
	{
		if (GGPlayerSettings.instance.Model.energy >= EnergyControlConfig.instance.maxEnergy)
		{
			return GGPlayerSettings.instance.Model.energy;
		}
		TimeSpan timeSpan = DateTime.Now.Subtract(new DateTime(GGPlayerSettings.instance.Model.lastTimeTookEnergy));
		if (timeSpan.TotalSeconds < (double)EnergyControlConfig.instance.secondsToRefreshPoint)
		{
			return GGPlayerSettings.instance.Model.energy;
		}
		float energyForTimespan = EnergyControlConfig.instance.GetEnergyForTimespan(timeSpan);
		float num = Mathf.Min(energyForTimespan, EnergyControlConfig.instance.maxEnergy - GGPlayerSettings.instance.Model.energy);
		return GGPlayerSettings.instance.Model.energy + num;
	}

	public void SpendEnergy(float energyToSpend)
	{
		RefreshEnergy();
		DateTime lastTimeTookEnergy = ((!(GGPlayerSettings.instance.Model.energy >= EnergyControlConfig.instance.maxEnergy)) ? new DateTime(GGPlayerSettings.instance.Model.lastTimeTookEnergy) : DateTime.Now);
		GGPlayerSettings.instance.UpdateEnergy(0f - energyToSpend, lastTimeTookEnergy);
	}

	public void SetEnergy(float energy)
	{
		RefreshEnergy();
		GGPlayerSettings.instance.SetEnergy(energy, new DateTime(GGPlayerSettings.instance.Model.lastTimeTookEnergy));
	}

	public void GainEnergy(float energyToGain)
	{
		RefreshEnergy();
		GGPlayerSettings.instance.UpdateEnergy(energyToGain, new DateTime(GGPlayerSettings.instance.Model.lastTimeTookEnergy));
	}
}
